package com.g5kj.coupon.ssm.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.g5kj.coupon.ssm.common.Result;
import com.g5kj.coupon.ssm.model.AdminEntity;
import com.g5kj.coupon.ssm.service.AdminService;
@Controller
@RequestMapping("/admin")
public class AdminController {
	
	@Resource
	private AdminService adminService;
	@RequestMapping("/login")
	public @ResponseBody Result login(@Param("name")String name,@Param("password")String password,HttpServletRequest request){
		//MD5Util.getMd5(password);
		System.out.println(name+"*********************");
		AdminEntity admin = adminService.login(name, password);
		if(admin!=null && admin.getIs_stop()==0){
			request.getSession().setAttribute("admin",admin);
			return Result.ok("登陆成功 ");
		}else{
			return Result.fail(admin==null?"账号或者密码错误":"账号已停用");
		}
		
	}
	@RequestMapping("/logout")
	public String logout(HttpSession session){
		session.invalidate();
		return "redirect:/login";
	}
	
	
	@RequestMapping("/list")
	public String adminList(HttpServletRequest request){
		List<AdminEntity> listAll = adminService.listAll();
		request.setAttribute("listAll", listAll);
		return "/user/adminList";
	}
	
	@RequestMapping("/show")
	public String showLogin(){
		return "login";
	}
	
	
	
	@RequestMapping("/add")
	public @ResponseBody Result add(AdminEntity adminEntity,HttpServletRequest request){
		System.out.println("add******");
		System.out.println(adminEntity.getId());
		if(adminEntity.getId()!=null){
			adminService.updateAdmin(adminEntity);
			System.out.println(adminEntity.getId());
			return Result.ok("修改成功");
		}else{
			Result result = adminService.addAdmin(adminEntity);
			
			return result;
		}
		
		
	}
	@RequestMapping("/showAdd")
	public String showAdd(Integer id,HttpServletRequest request){
		if(id!=null){
			request.setAttribute("admin",adminService.getAdminById(id));
			return "/user/add";
		}else{
			System.out.println("************");
			return "/user/add";
		}
		
	}
	
	@RequestMapping("/deleteAdmin")
	public @ResponseBody Result delete(int id){
		System.out.println(id);
		int result = adminService.delete(id);
		if(result>0){
			return Result.ok("删除成功");
		}else{
			return Result.fail("删除失败");
		}
		
	}
	
	@RequestMapping("/stop")
	public @ResponseBody Result is_stop(int id){
		System.out.println(id);
		return adminService.stop(id);
	}
	
	@RequestMapping("/start")
	public @ResponseBody Result start(int id){
		return adminService.start(id);
	}
	
	
	
	
	
	
	
	
	

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}
	
	

}
